package de.tum.in.eist.poll.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RadioButton;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.Widget;

import de.tum.in.eist.poll.shared.Feedback;
import de.tum.in.eist.poll.shared.Lecture;

/**
 * Feedback Webpage
 */
public class Poll extends Webpage {

	private static PollUiBinder uiBinder = GWT.create(PollUiBinder.class);

	@UiField
	RadioButton question1a;
	@UiField
	RadioButton question1c;
	@UiField
	RadioButton question1b;
	@UiField
	RadioButton question1d;
	@UiField
	RadioButton question1e;
	@UiField
	TextArea question2;
	@UiField
	TextArea question3;
	@UiField
	Button button;

	interface PollUiBinder extends UiBinder<Widget, Poll> {
	}

	/**
	 * Identification of the lecture.
	 */
	private String selectedLecture = null;

	/**
	 * 
	 * @param lectureID
	 *            ID of the choosen Lecture
	 */
	public Poll(final String lectureID) {
		selectedLecture = lectureID;
	}

	@Override
	public final void loadWebsite() {
		setContent(uiBinder.createAndBindUi(this));
	}

	/**
	 * Sending feebback to server.
	 * 
	 * @param event
	 *            Clickevent
	 */
	@UiHandler("button")
	final void onButtonClick(final ClickEvent event) {
		if (!(question1a.getValue() || question1b.getValue()
				|| question1c.getValue() || question1d.getValue() || question1e
				.getValue())) {
			com.google.gwt.user.client.Window
					.alert("No Satisfiability choosen!");
			return;
		}
		Feedback feedback = new Feedback(new Lecture(selectedLecture, "", null, null));
		feedback.setQuestion1(question1a.getValue() ? 1
				: question1b.getValue() ? 2 : question1c.getValue() ? 3
						: question1d.getValue() ? 4 : 5);
		feedback.setQuestion2(question2.getText());
		feedback.setQuestion3(question3.getText());

		AsyncCallback<Boolean> callback = new AsyncCallback<Boolean>() {

			@Override
			public void onFailure(final Throwable caught) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onSuccess(final Boolean result) {
				Label label = new Label("Feedback successfully submitted!");
				setContent(label);
			}
		};
		getService().submitFeedback(feedback, callback);
	}
}
